export default {
  // 开启命名空间
  namespaced: true,
  // 数据
  state: () => ({
    address: JSON.parse(uni.getStorageSync('address') || '{}'),
    token: uni.getStorageSync('token') || '',
    userinfo: JSON.parse(uni.getStorageSync('userinfo') || '{}'),
    redirectInfo: null
  }),
  
  // 方法
  mutations: {
    updataRedirectInfo(state, info) {
      state.redirectInfo = info
    },
    updataToken(state,token) {
      state.token = token
      this.commit('my_user/saveTokenToStor')
    },
    saveTokenToStor(state){
      uni.setStorageSync('token',JSON.stringify(state.token))
    },
    updataUserinfo(state,userinfo) {
      state.userinfo = userinfo
      this.commit('my_user/saveUserinfoToStorage')
    },
    saveUserinfoToStorage(state){
      uni.setStorageSync('userinfo',JSON.stringify(state.userinfo))
    },
    // 更新收货地址
    updataAddress(state,address) {
      state.address = address
      this.commit('my_user/saveAddressToStorage')
    },
    // 持久化address
    saveAddressToStorage(state) {
      uni.setStorageSync('address',JSON.stringify(state.address))
    }
  },
  getters: {
    addr (state) {
      if (state.address.countyName === '') {
        return ''
      } else {
        return state.address.provinceName + state.address.cityName +state.address.countyName +state.address.detailInfo
      }
    }
  }
} 